package com.wiz.weixin.manager;

import java.io.File;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.wiz.weixin.tencent.vo.WxButton;
import com.wiz.weixin.tencent.vo.WxClickButton;
import com.wiz.weixin.tencent.vo.WxComlexButton;
import com.wiz.weixin.tencent.vo.WxMenu;
import com.wiz.weixin.tencent.vo.WxViewButton;

/**
 * 8583格式的XML解析
 * @author Hakeny
 */
public class WxMenuXmlParseFactory {

	private String fileurl;
	private Map<String,WxMenu> menuMap;

	/**
	 * 初始化
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public void init() throws Exception {
		URL url = WxMenuXmlParseFactory.class.getResource("/");
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(URLDecoder.decode(url.getPath(),"UTF-8")+fileurl));
		Element root = document.getRootElement();
		Iterator<Element> itRoot = root.elementIterator();
		menuMap = new HashMap<String,WxMenu>();
		while(itRoot.hasNext()){
			Element paramNode = itRoot.next();//解读到 menu位置
			WxMenu menu = new WxMenu();
			List<WxComlexButton> mainButtonList = new ArrayList<WxComlexButton>();
			String menuName = paramNode.attribute("name").getText();
			Iterator<Element> itButton = paramNode.elementIterator();
			while(itButton.hasNext()){
				Element elementFiled = itButton.next();//解读到button位置
				String name = elementFiled.attributeValue("name");
				WxComlexButton comlexButton = new WxComlexButton();
				comlexButton.setName(name);
				List<WxButton> subButtonList = new ArrayList<WxButton>();
				Iterator<Element> itSubButton = elementFiled.elementIterator();
				while(itSubButton.hasNext()){
					Element subButton = itSubButton.next();//解读到sub_button位置
					String type = subButton.attributeValue("type");
					//时间原因暂时只定义这2种 --- 2015.8.17
					if("view".equals(type)){
						WxViewButton view = new WxViewButton();
						String subButtonName = subButton.attributeValue("name");
						String turl = subButton.attributeValue("url");
						view.setName(subButtonName);
						view.setUrl(turl);
						view.setType(type);
						subButtonList.add(view);
					}else if("click".equals(type)){
						WxClickButton button = new WxClickButton();
						String subButtonName = subButton.attributeValue("name");
						String key = subButton.attributeValue("key");
						button.setName(subButtonName);
						button.setKey(key);
						button.setType(type);
						subButtonList.add(button);
					}else{
						WxClickButton button = new WxClickButton();
						String subButtonName = subButton.attributeValue("name");
						String key = subButton.attributeValue("key");
						button.setName(subButtonName);
						button.setKey(key);
						button.setType(type);
						subButtonList.add(button);
					}
				}
				WxButton[] sonButtons = new WxButton[subButtonList.size()];
				for(int i=0;i<subButtonList.size();i++){
					sonButtons[i] = subButtonList.get(i);
				}
				comlexButton.setSub_button(sonButtons);
				mainButtonList.add(comlexButton);
			}
			WxButton[] mainButtons = new WxComlexButton[mainButtonList.size()];
			for(int i=0;i<mainButtonList.size();i++){
				mainButtons[i] = mainButtonList.get(i);
			}
			menu.setButton(mainButtons);
			//put in
			menuMap.put(menuName, menu);
		}
	}
	
	

	public Map<String, WxMenu> getMenuMap() {
		return menuMap;
	}

	public String getFileurl() {
		return fileurl;
	}

	public void setFileurl(String fileurl) {
		this.fileurl = fileurl;
	}
}
